
local rufo, beni, tig = "rufo__feedstar", "beni__feedstar", "tig__feedstar"

local skel = fk.CreateSkill {
  name = "emo__feedstar",
  dynamic_desc = function (self, player, lang)
    local arg1, arg2, arg3 = "", "", ""
    if player.general == rufo or player.deputyGeneral == rufo then
      arg1 = "emo__feedstar_lubo"
    end
    if player.general == beni or player.deputyGeneral == beni then
      arg2 = "emo__feedstar_banni"
    end
    if player.general == tig or player.deputyGeneral == tig then
      arg3 = "emo__feedstar_tige"
    end
    return "emo__feedstar_inner:" .. arg1 .. ":" .. arg2 .. ":" .. arg3
  end,
}

local desc = "<a href='emo_feedfamily'>宗族技</a>，出牌阶段限X次，你可以重铸X张牌（至少Y张装备牌），视为使用一张【决斗】（X为你攻击范围，Y为本阶段发动次数）"
local rufo_desc = "<br>卢波专属：同族角色可以重铸锦囊牌代替重铸装备牌，令此牌的响应量翻倍。"
local beni_desc = "<br>班尼专属：若你因此受到伤害，令一名同族角色摸一张【酒】或伤害牌。"
local tig_desc = "<br>提格专属：你可以摸一张牌并交换使用者与目标，则结算后你分配对方打出的【杀】给同族角色。"

Fk:loadTranslationTable{
  ["emo__feedstar"] = "饲料stars出动！",
  [":emo__feedstar"] = desc .. rufo_desc .. beni_desc .. tig_desc,

  [":emo__feedstar_inner"] = desc.."{1}{2}{3}",
  ["emo__feedstar_lubo"] = rufo_desc,
  ["emo__feedstar_banni"] = beni_desc,
  ["emo__feedstar_tige"] = tig_desc,

  ["#emo__feedstar"] = "重铸 %arg 张牌，视为使用【决斗】",
  ["#emo__feedstar-equip"] = "重铸 %arg 张牌(需含%arg2张装备牌)，视为使用【决斗】",
  ["#emo__feedstar-nonbasic"] = "重铸 %arg 张牌(需含%arg2张非基本牌)，视为使用【决斗】",
  ["emo_feedfamily"] = "宗族成员：提格，卢波，班尼",
  ["#emo__feedstar-draw"] = "令一名同族角色摸一张【酒】或伤害牌",
  ["#emo__feedstar-exchange"] = "你可以摸一张牌并交换使用者与目标，则结算后你分配双方打出的【杀】给同族角色",
  ["#emo__feedstar-yiji"] = "你分配双方打出的【杀】给同族角色",

  ["$emo__feedstar1"] = "", -- 班尼1
  ["$emo__feedstar2"] = "", -- 班尼2
  ["$emo__feedstar3"] = "", -- 卢波1
  ["$emo__feedstar4"] = "看看卢波的精彩表现！", -- 卢波2
  ["$emo__feedstar5"] = "剑圣要揭晓了！", -- 提格1
  ["$emo__feedstar6"] = "", -- 提格2
}

--- 是否为宗族成员
local isFamily = function (p)
  local list = {
    rufo,
    beni,
    tig,
  }
  return table.contains(list, p.general) or table.contains(list, p.deputyGeneral)
end

-- 卢波光环，可以重铸锦囊牌
local luboBuff = function (player)
  return isFamily(player) and table.find(Fk:currentRoom().alive_players, function (p)
    return p.general == rufo or p.deputyGeneral == rufo
  end) ~= nil
end

skel:addEffect("viewas", {
  anim_type = "offensive",
  mute_card = true,
  prompt = function (self, player)
    local x = player:getAttackRange()
    local y = player:usedSkillTimes(skel.name, Player.HistoryPhase)
    if y > 0 then
      if luboBuff(player) then return "#emo__feedstar-nonbasic:::"..x..":"..y end
      return "#emo__feedstar-equip:::"..x..":"..y
    end
    return "#emo__feedstar:::"..x
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected < player:getAttackRange()
  end,
  view_as = function (self, player, cards)
    if #cards == 0 or #cards ~= player:getAttackRange() then return nil end
    local equips = table.filter(cards, function (id)
      local t = Fk:getCardById(id).type
      if luboBuff(player) then
        return t ~= Card.TypeBasic
      else
        return t == Card.TypeEquip
      end
    end)
    if player:usedSkillTimes(skel.name, Player.HistoryPhase) > #equips then return nil end
    local card = Fk:cloneCard("duel")
    card.skillName = skel.name
    card:addFakeSubcards(cards)
    return card
  end,
  on_cost = function (self, player, data)
    local a, b = 1, 6 -- 语音
    if player.general == beni or player.deputyGeneral == beni then
      a, b = 1, 2
    elseif player.general == tig or player.deputyGeneral == tig then
      a, b = 5, 6
    elseif player.general == rufo or player.deputyGeneral == rufo then
      a, b = 3, 4
    end
    return { audio_index = {a, b} }
  end,
  before_use = function (self, player, use)
    local room = player.room
    local x = player:usedSkillTimes(skel.name, Player.HistoryPhase) -- 使用次数，第一次为1
    use.extra_data = use.extra_data or {}
    use.extra_data.emo__feedstar_user = player
    local subcards = table.simpleClone(use.card.fake_subcards)
    room:recastCard(subcards, player, skel.name)
    if x > 1 and luboBuff(player) and table.find(subcards, function (id)
      return Fk:getCardById(id).type == Card.TypeTrick
    end) then
      use.extra_data.emo__feedstar_double = true
    end
    if not player.dead and #use.tos > 0 and (player.general == tig or player.deputyGeneral == tig)
     and room:askToSkillInvoke(player, {
      skill_name = skel.name, prompt = "#emo__feedstar-exchange",
    }) then
      use.extra_data.emo__feedstar_fenpei = player
      player:drawCards(1, skel.name)
      local to = use.tos[1]
      if to then
        use.from = to
        use.tos[1] = player
      end
    end
  end,
  times = function (self, player)
    if player.phase ~= Player.Play then return -1 end
    return player:getAttackRange() - player:usedSkillTimes(skel.name, Player.HistoryPhase)
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) < player:getAttackRange()
  end,
  enabled_at_response = function (self, player, response)
    return not response and player.phase == Player.Play
  end,
})

skel:addEffect(fk.TargetSpecified, {
  can_refresh = function(self, event, target, player, data)
    return table.contains(data.card.skillNames, skel.name) and target == player
    and data.extra_data and data.extra_data.emo__feedstar_double
  end,
  on_refresh = function(self, event, target, player, data)
    data:setResponseTimes(data:getResponseTimes() * 2)
  end,
})

skel:addEffect(fk.Damaged, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and data.card and
    table.contains(data.card.skillNames, skel.name)
    and (player.general == beni or player.deputyGeneral == beni)
  end,
  on_cost = function (self, event, target, player, data)
    local targets = table.filter(player.room.alive_players, function (p)
      return p == player or isFamily(p)
    end)
    local tos = player.room:askToChoosePlayers(player, {
      min_num = 1, max_num = 1, targets = targets, skill_name = skel.name,
      prompt = "#emo__feedstar-draw",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local cards = table.filter(room.draw_pile, function (id)
      local c = Fk:getCardById(id)
      return c.is_damage_card or c.trueName == "analeptic"
    end)
    if #cards > 0 then
      room:obtainCard(to, table.random(cards), true, fk.ReasonJustMove, player, skel.name)
    end
  end,
})

skel:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and data.extra_data and data.extra_data.emo__feedstar_fenpei == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = {}
    local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    if not use_event then return end
    room.logic:getEventsByRule(GameEvent.RespondCard, 1, function (e)
      local resp = e.data
      if resp.from ~= player and resp.responseToEvent and resp.responseToEvent.card == data.card then
        table.insertTableIfNeed(ids, Card:getIdList(resp.card))
      end
    end, use_event.id)
    ids = table.filter(ids, function (id)
      return room:getCardArea(id) == Card.DiscardPile
    end)
    if #ids > 0 then
      local targets = table.filter(player.room.alive_players, function (p)
        return p == player or isFamily(p)
      end)
      room:askToYiji(player, {
        min_num = 0, max_num = #ids, skill_name = skel.name, prompt = "#emo__feedstar-yiji",
        cards = ids, expand_pile = ids, targets = targets,
      })
    end
  end,
})

return skel
